package controllers;

import models.user.LinkUserInfo;
import models.user.UserInfo;

import org.codehaus.jackson.JsonNode;

import play.mvc.Controller;
import play.mvc.Result;
import utils.ControllerUtils;
import utils.MengException;
import utils.StringUtils;

public class UserSafe extends Controller{
	
	public static Result forgetPwd() {

		JsonNode node = request().body().asJson();
		String email = node.findPath("email").asText();

		try {
			boolean flag = UserInfo.getInstances().forgetPwd(email);
			return ok(StringUtils.toJson("status", flag));
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}
	}

	public static Result modifyPwd() {

		UserInfo ui = (UserInfo)ControllerUtils.getParams(UserInfo.class);
		ui.username = session().get("username");

		try {
			UserInfo.getInstances().modifyPwd(ui.username, ui.password, "", false);
			return redirect("/mysafe?tab=password"); 
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}
	}
	
	public static Result activeMobile() {

		LinkUserInfo lui = (LinkUserInfo)ControllerUtils.getParams(LinkUserInfo.class);
		lui.username = session().get("username");

		try {
			LinkUserInfo.getInstances().activeMobile(lui.username, lui.mobile);
			return redirect("/mysafe?tab=mobile");
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}
	}

	public static Result activeEmail() {

		LinkUserInfo lui = (LinkUserInfo)ControllerUtils.getParams(LinkUserInfo.class);
		lui.username = session().get("username");
		try {
			LinkUserInfo.getInstances().activeEmail(lui.username, lui.email);
			return redirect("/mysafe?tab=email");
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}
	}
	
}
